/*
 * @Author: ChunLai
 * @Date: 2022-09-06 17:03:37
 * @LastEditTime: 2025-08-06 16:05:02
 * @Description: 缓存
 * @FilePath: \02.bldinsure\src\methods\cache.js
 */
import { getDataFromUrl } from "@/utils";
export const setMessage = (key, value) => {
  let valType = Object.prototype.toString
    .call(value)
    .slice(8, -1)
    .toLowerCase();
  if (valType === 'object' || valType === 'array') {
    sessionStorage.setItem(key, JSON.stringify(value));
  } else {
    sessionStorage.setItem(key, value);
  }
}

export const getMessage = (key) => {
  let msg = sessionStorage.getItem(key);
  if (msg === null) {
    // 没有存储值
    return msg;
  } else {
    let msgType = msg.substr(0, 1);
    if (msgType === '{' || msgType === '[') {
      return JSON.parse(msg);
    } else {
      return msg;
    }
  }
}

export const getCacheValue = (key, subKey) => {
  const store = getMessage("bld_insure") || {},
    state = store[key] || {};
  if (!subKey) return JSON.parse(JSON.stringify(state));
  let subValue = state[subKey] || "";
  if (subValue === "") {
    if (subKey === "compEngName") {
      subValue = getDataFromUrl("company");
    }
    if (subKey === "mainRiskCode") {
      subValue = getDataFromUrl("mcode");
    }
    if (subKey === "workNum") {
      subValue = getDataFromUrl("workNum");
    }
  }
  return JSON.parse(JSON.stringify(subValue));
};